import { createSlice } from '@reduxjs/toolkit'
import { realm, schemaName } from "../../utils/schema"

const initialState = {
    browserSearchLogs: [],
    browserPopularLogs: [],
}
export const dappSlice = createSlice({
    name: 'dapp',
    initialState,
    reducers: {
        SetBrowserSearchLogs: (state, action) => {
            state.browserSearchLogs = action.payload
        },
        SetBrowserPopularLogs: (state, action) => {
            state.browserPopularLogs = action.payload
        },
    },
})

export const { SetBrowserSearchLogs, SetBrowserPopularLogs } = dappSlice.actions

export const asyncGetBrowserSearchLogs = () => async (dispatch) => {
    let list = realm.objects(schemaName.BrowserSearchLogs).sorted('time', true);
    dispatch(SetBrowserSearchLogs(JSON.parse(JSON.stringify(list))));
};

export const asyncGetBrowserPopularLogs = () => async (dispatch) => {
    let list = realm.objects(schemaName.BrowserPopularLogs).sorted('time', true);
    dispatch(SetBrowserPopularLogs(JSON.parse(JSON.stringify(list))));
};


export default dappSlice.reducer